<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/2/20
 * Time: 10:50
 */

namespace app\admin\controller;


use think\Db;
use think\Request;
class School extends Base {
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($name)&&$name!="")
            {
                $map['a.school_name'] = ['like','%'.$name.'%'];
            }
            if(isset($province)&&$province!="")
            {
                $map['a.school_sheng'] = $province;
            }

            if(isset($city)&&$city!="")
            {
                $map['a.school_shi'] = $city;
            }
            if(isset($district)&&$district!="")
            {
                $map['a.school_qu'] = $district;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('school')->alias('a')
                ->where($map)
//                ->join('up_area ua','ua.district_id = a.school_sheng','left')
//                ->join('up_area ua1','ua1.district_id= a.school_shi','left')
//                ->join('up_area ua2','ua2.district_id= a.school_qu','left')
                ->count();// 获取总条数
            $lists = Db::name('school')->alias('a')
//                ->join('up_area ua','ua.district_id = a.school_sheng','left')
//                ->join('up_area ua1','ua1.district_id = a.school_shi','left')
//                ->join('up_area ua2','ua2.district_id = a.school_qu','left')
//                ->order()
                ->page($NowPage,$limits)
                ->where($map)
//                ->field('a.*,ua.district,ua1.district as district1,ua2 district as district2')
                ->field('a.*')
                ->select();
//            foreach ($lists as $key=>&$vo){
//                    if($vo['school_status']==1){
//                        $vo['school_status_name'] = '上架';
//                    }else{
//                        $vo['school_status_name'] = '下架';
//                    }
//                $sheng = Db::table('up_area')->where('district_id',$vo['school_sheng'])->value('district');
//                $shi = Db::table('up_area')->where('district_id',$vo['school_shi'])->value('district');
//                $qu = Db::table('up_area')->where('district_id',$vo['school_qu'])->value('district');
//                $vo['address'] = $sheng.$shi.$qu;
//            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }

        $province = Db::table('up_area')->where('level', 1)->select();
        $this->assign([
            'province' => $province,
        ]);
        return $this->fetch();
    }
    /**
     * 改展示状态（是否展示过去年份）
     * @author
     */
    public function changeSchoolShow()
    {
        extract(input());
        $res = Db::table('up_school')
            ->where('school_id',$id)
            ->update([
                'school_update_time' => date('Y-m-d H:i:s'),
                'school_history' => $num
            ]);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    /**
     * 改现场调换状态（是否开启现场调换）
     * @author
     */
    public function changeSchoolChange()
    {
        extract(input());
        $res = Db::table('up_school')
            ->where('school_id',$id)
            ->update([
                'school_update_time' => date('Y-m-d H:i:s'),
                'school_change' => $num
            ]);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    /**
     * 改学校状态（是否冻结学校）
     * @author
     */
    public function changeSchoolStatus()
    {
        extract(input());
        $res = Db::table('up_school')
            ->where('school_id',$id)
            ->update([
                'school_update_time' => date('Y-m-d H:i:s'),
                'school_status' => $num
            ]);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    /**
     * 禁止年份
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'school_no_years'=>input('value'),
        ];
        $res = Db::name('school')->where('school_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    //---------------------------------------------------------------------
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function tong(){
        extract(input());
        if(request()->isAjax()){
            extract(input());
            $map = array();
            $map['a.st_shool_id'] = $school_id;
            if(isset($name)&&$name!="")
            {
                $map['a.st_tong_erp_number'] = ['like','%'.$name.'%'];
            }

            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('school_tong')->alias('a')
                ->where($map)
                ->count();// 获取总条数
            $lists = Db::name('school_tong')->alias('a')
                ->order('st_time desc')
                ->page($NowPage,$limits)
                ->where($map)
//                ->field('a.*,ua.district,ua1.district as district1,ua2 district as district2')
                ->field('a.*')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }

        $this->assign([
            'school_id' => $school_id,
        ]);
        return $this->fetch();
    }
}